using System;
using System.IO;
using Microsoft.Xna.Framework;

namespace ChildrenOfWar
{
    class COWLevelIO
    {
        static public COWLevel LoadLevel(String FileURL)
        {
            BinaryReader LevelFile;
            //open the file stream and wrap it in a binary reader
            try
            {
                LevelFile = new BinaryReader(TitleContainer.OpenStream(FileURL));//use title container becuse... Microsoft is dumb?
            }
            catch(FileNotFoundException e)//If the file is not found, catch the error and print an error message.
            {
                Console.Error.WriteLine("Error File: " + FileURL + " could not be found! Returning null...");
                return null;
            }

            //create an empty level to put info into
            COWLevel LoadingLevel = new COWLevel();

            //Read ints for Starting Y, Ending Y, and number of children and add them to the level then read the URL for the background and add it. 
            LoadingLevel.StartY = LevelFile.ReadByte();
            LoadingLevel.EndY = LevelFile.ReadByte();
            LoadingLevel.ChildNum = LevelFile.ReadByte();
            LoadingLevel.BackgroundURL = LevelFile.ReadString();

            //Read Location of objects, enemies

            LevelFile.Close();

            return LoadingLevel;
        }

        static public COWLevel LoadLevelASCII(String FileURL)
        {
            StreamReader LevelFile;
            //open the file stream and wrap it in a binary reader
            try
            {
                LevelFile = new StreamReader(TitleContainer.OpenStream(FileURL));//use title container becuse... Microsoft is dumb?
            }
            catch (FileNotFoundException e)//If the file is not found, catch the error and print an error message.
            {
                Console.Error.WriteLine("Error File: " + FileURL + " could not be found! Returning null...");
                return null;
            }

            //create an empty level to put info into
            COWLevel LoadingLevel = new COWLevel();

            //Read ints for Starting Y, Ending Y, and number of children and add them to the level then read the URL for the background and add it. 
            LoadingLevel.StartY = Convert.ToByte(LevelFile.ReadLine());
            LoadingLevel.EndY = Convert.ToByte(LevelFile.ReadLine());
            LoadingLevel.ChildNum = Convert.ToByte(LevelFile.ReadLine());
            LoadingLevel.BackgroundURL = LevelFile.ReadLine();

            //Read Location of objects, enemies

            LevelFile.Close();

            return LoadingLevel;
        }

        static public void SaveLevel(COWLevel levelToSave, String FileURL)
        {
            //Note, Output stream does not work on Windows phone. This is for converting to a binary file for final packageing. DO NOT USE THIS OR I EAT YOUR FACE!!
            BinaryWriter WriteFile;
            //open the file stream and wrap it in a binary reader
            try
            {
                WriteFile = new BinaryWriter(new FileStream(FileURL, FileMode.OpenOrCreate));//use title container becuse... Microsoft is dumb?
            }
            catch (IOException e)//If the file is not found, catch the error and print an error message.
            {
                Console.Error.WriteLine("Error File: " + FileURL + " could not be opened!");
                return;
            }

            //write Starting Y, Ending Y, number of children, and then the URL for the background. 
            WriteFile.Write(levelToSave.StartY);
            Console.Out.WriteLine("Printing: "+levelToSave.StartY);
            WriteFile.Write(levelToSave.EndY);
            Console.Out.WriteLine("Printing: " + levelToSave.EndY);
            WriteFile.Write(levelToSave.ChildNum);
            Console.Out.WriteLine("Printing: " + levelToSave.ChildNum);
            WriteFile.Write(levelToSave.BackgroundURL);
            Console.Out.WriteLine("Printing: " + levelToSave.BackgroundURL);

            //Write Location of objects, write enemies, etc. after this

            WriteFile.Close();
        }

        /*private COWEnemy LoadEnemy()
        {
        }*/

        /*private COWActor LoadEnvObject()
        {
        }*/
    }
}
